package com.corewillsoft.fireflies.gamelaws;

import com.corewillsoft.fireflies.gameobjects.Firefly;
import com.corewillsoft.fireflies.gameobjects.GameObject;

/**
 * @author Kof
 * Date: 25.11.12
 * Time: 19:36
 */

/**
 * Inertia law works on fireflies, it decreases their current speed value
 */
public class InertiaLaw extends BaseLaw {
    /** factor of speed of slowdown */
    private final float SLOWDOWN_RATE = 20f;

    @Override
    public void applicate(GameObject gameObject) {
        if (gameObject instanceof Firefly) {
            applicateFirefly((Firefly) gameObject);
        }
    }

    @Override
    public LawType getType() {
        return LawType.INERTIA;
    }
    
    private void applicateFirefly(Firefly firefly) {
        if (firefly.isActive()) {
            Float speedX = firefly.getSpeedX();
            Float speedY = firefly.getSpeedY();

            Float newSpeedX = Math.signum(speedX)
                    * (Math.abs(speedX) - Math.abs(speedX) / SLOWDOWN_RATE);
            Float newSpeedY = Math.signum(speedY)
                    * (Math.abs(speedY) - Math.abs(speedY) / SLOWDOWN_RATE);

            firefly.setSpeed(newSpeedX, newSpeedY);
        }
    }
}
